<template>
  <div class="">
    <el-card>
       <el-form :inline="true">
        <el-form-item label="关键字">
          <el-input style="200px" ></el-input>
        </el-form-item>
         <el-form-item label="状态">
        <el-select v-model="params.state" placeholder="请选择"  >
         <el-option label="已启用" value="1"></el-option>
         <el-option label="已禁用" value="0"></el-option>
        </el-select>
       </el-form-item>
        <el-form-item >
          <el-button @click="qinchu" >清除</el-button>
          <el-button type="primary" @click="sousuo">搜索</el-button>
        </el-form-item>

        <el-form-item class="right" >
          <el-button type="primary" @click="dialogVisible=true">新增目录</el-button>
        </el-form-item>
       </el-form>
      <el-alert type="info" class="alert" :closable="false" show-icon
        >数据一共{{ this.total }}条</el-alert
      >
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>

        <el-table-column prop="directoryName" label="标签名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期"> </el-table-column>
        <el-table-column prop="totals" label="面试题数量"> </el-table-column>
        <el-table-column label="状态">
          <template v-slot="props">
            {{ state[props.row.state] }}
          </template>
        </el-table-column>
       <template >
          <el-table-column label="操作">
         <template v-slot="props">
           <el-button type="text" @click="toggleState(props.row)" >{{ props.row.state===1?'禁用':'启用' }}</el-button>
           <el-button type="text" :disabled="props.row.state===1">修改</el-button>
           <el-button type="text" @click="del(props.row)" :disabled="props.row.state===1">删除</el-button>
         </template>
        </el-table-column>
       </template>
      </el-table>
      <div class="fenye">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-size="params.pagesize"
          layout="sizes, prev, pager, next"
          :total="total"
          :current-page="params.page"
          :page-sizes="[5, 10, 20]"
        >
        </el-pagination>
      </div>
    </el-card>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"

    >
    <el-form  :data="selectList"  label-width="80px" >
      <el-form-item label="所属学科" >
        <el-select v-model="value" placeholder="请选择"  @visible-change="selectChange">
          <el-option
          v-for="(item,index) in selectList"
          :key="index"
          :label="item.label"
          :value="item.value" >

          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" >
        <el-input v-model="inputname"></el-input>
      </el-form-item>
    </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary"
          @click="isOk">确 定</el-button
        >
      </span>
    </el-dialog>
  </div>

  <!-- 弹出框 -->
</template>

<script>
import { list, add, remove, changeState, simple } from '@/api/hmmm/directorys.js'
export default {
  name: '',
  components: {

  },
  props: {

  },
  data () {
    return {
      inputname: '',
      value: null,
      tableData: [],
      selectList: [],
      params: {
        page: 1,
        pagesize: 10,
        state: null
      },
      total: null,
      state: {
        1: '已启用',
        0: '已禁用'
      },
      input: '',
      alertText: '',
      dialogVisible: false

    }
  },
  created () {
    this.catalogList()
  },
  methods: {
    async catalogList () {
      const { data } = await list(this.params)

      // console.log(data)
      this.tableData = data.items
      this.tableData.subjectName = this.optionsList
      this.total = data.counts
      // console.log(this.tableData)
    },

    handleSizeChange (val) {
      this.params.page = 1
      this.params.pagesize = val
      this.catalogList()
    },
    handleCurrentChange (val) {
      this.params.page = val
      this.catalogList()
    },
    async isOk () {
      await add({
        id: null,
        subjectID: this.value,
        directoryName: this.inputname
      })
      this.catalogList()
      this.dialogVisible = false
      this.value = ''
      this.inputname = ''
    },
    async toggleState (article) {
      await changeState({
        id: article.id,
        state: article.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      article.state = article.state === 1 ? 0 : 1
    },
    async selectChange () {
      const data = await simple()
      this.selectList = data.data
      console.log(this.selectList)
    },

    async del (id) {
      await remove(id)
      this.catalogList()
    },
    sousuo () {
      this.params.page = 1
      this.catalogList()
    },
    qinchu () {
      this.params.state = null
      this.params.page = 1
      this.catalogList()
    }

  }
}
</script>

<style  scoped>
.fenye {
  margin-top: 25px;
}
/* .fenye input{
  position: relative;
  top: 10px;
} */

.right{
  float: right;
}
</style>
